<template>
  <el-dialog :title="tableTitle" v-model="props.dialogVisible" width="35%" top="15vh" center @open="tableOpen"
    :before-close="cancelClick">
    <span v-if="titleIDText" class="titleID">（{{ titleIDText }}）</span>
    <div class="bodyBox">
      <el-table class="tableCss" v-loading="tableLoading" border :data="ruleForm"
        :default-sort="{ prop: 'date', order: 'descending' }" height="calc(100vh - 475px)"
        :show-overflow-tooltip="true">
        <el-table-column fixed align="center" label="序号" width="60">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="lang" label="语言" width="">
          <template #default="scope">
            {{ scope.row.lang }}
          </template>
        </el-table-column>
        <el-table-column prop="product_name" label="产品名称" width="">
          <template #default="scope">
            <el-input v-model="scope.row.product_name" placeholder="请输入" style="width: 92%;" /><span
              v-if="scope.$index + 1 === 1" style="color: red;">*</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="上架状态" width="">
          <template #default="scope">
            <el-select v-model="scope.row.status" placeholder="上架状态" style="">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelClick" :loading="btnLoading">关 闭</el-button>
      <el-button type="primary" @click="submitForm()" :loading="btnLoading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, defineProps } from 'vue'
import cloneDeep from 'lodash/cloneDeep';
import type { FormInstance } from 'element-plus'
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false,
  },
  tableLoading: {
    type: Boolean,
    default: false,
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
  tableTitle: {
    type: String,
    default: '',
  },
  titleIDText: {
    type: String,
    default: '',
  },
  ruleForm: {
    type: Array,
    default: () => [],
  },
})
interface RuleForm {
  lang: string;
  product_name: string;
  status: string;
}
let ruleFormData = ref<RuleForm[]>([
  { lang: '', product_name: '', status: '0' },

]);
const options = [
  {
    value: '0',
    label: '已下架',
  },
  {
    value: '1',
    label: '已上架',
  },
]
// 弹窗打开事件
const tableOpen = () => {
  // Object.assign(ruleFormData, props.ruleForm);
}
const ruleFormRef = ref<FormInstance>()
const emits = defineEmits(['beforeClose', 'submitForm']);
// 确定按钮
const submitForm = () => {
  ruleFormData = cloneDeep(props.ruleForm)
  // console.log(111, ruleFormData);

  ruleFormData[0].lang = 'cn'
  ruleFormData[1].lang = 'en'
  if (!ruleFormData[0].product_name) {
    return ElMessage.error("请输入中文产品名称")
  }
  emits('submitForm', ruleFormData, props.tableTitle);
}
// 弹窗关闭事件
const cancelClick = () => {
  // 清空表单
  ruleFormRef.value?.resetFields();
  emits('beforeClose', false);
}
// 显式暴露方法给父组件
defineExpose({
  cancelClick
});
</script>
<style scoped lang="scss">
.titleID {
  width: 100%;
  display: inline-block;
  margin: 10px auto;
  text-align: right;
}

.tableCss {
  width: 100%;

  .doubleRow {
    color: #bababb;
  }
}
</style>